package vn.vfriends.vbox.rest;

import java.util.ArrayList;
import vn.vfriends.vbox.storage.GoogleDriveService;
import vn.vfriends.vbox.storage.DropboxService;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.interceptor.Interceptors;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import vn.vfriends.vbox.facade.AccountFacade;
import vn.vfriends.vbox.interceptor.LoggingInterceptor;
import vn.vfriends.vbox.model.Storage;
import vn.vfriends.vbox.model.Account;
import vn.vfriends.vbox.model.File;
import vn.vfriends.vbox.model.Provider;

/**
 *
 * @author tuancn
 */
@Interceptors(LoggingInterceptor.class)
@Stateless
@Path("storages")
public class StorageService {
    
    private static final Logger logger = LoggerFactory.getLogger(StorageService.class);
    
    @Inject
    private GoogleDriveService googleService;
    
    @Inject 
    private AccountFacade accountFacade;
    
    @Inject
    private DropboxService dropboxService;
    
    @GET
    @Path("{id: \\d+}")
    @Produces({"application/xml", "application/json"})
    public Storage getStorage(@PathParam("id") Integer id) {
        Account account = this.accountFacade.find(id);
        if(account != null) {
            Storage storage = null;
            try {
                if (account.getProvider() == Provider.GOOGLE) {
                    storage = googleService.getStorage(account.getAccessToken());
                } else if (account.getProvider() == Provider.DROPBOX) {
                    storage = dropboxService.getStorage(account.getAccessToken());
                }
            } catch (WebApplicationException e) {
                logger.error(e.getMessage(), e);
                throw new WebApplicationException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR);
            }

            if (storage == null) {
                throw new WebApplicationException("The storage is not exist", Response.Status.NOT_FOUND);
            }
            return storage;
        } else {
            throw new WebApplicationException("The account is not exist", Response.Status.NOT_FOUND);
        }
    }
    
    @GET
    @Path("files")
    @Produces({"application/xml", "application/json"})
    public List<File> getFiles(@QueryParam("accountId") Integer accountId, @QueryParam("fileId") String fileId) {
        Account account = this.accountFacade.find(accountId);
        if(account != null) {
            List<File> files = new ArrayList<>();
            try {
                if (account.getProvider() == Provider.GOOGLE) {
                    files = googleService.getFiles(account.getAccessToken(), fileId);
                } else if (account.getProvider() == Provider.DROPBOX) {
                    files = dropboxService.getFiles(account.getAccessToken(), fileId);
                }
                
                return files;
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                throw new WebApplicationException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR);
            }
        } else {
            throw new WebApplicationException("The account is not exist", Response.Status.NOT_FOUND);
        }
    }
}
